package net.tv90.volunteerserveradmin.manager;

import jakarta.annotation.Resource;
import net.tv90.volunteerserveradmin.utils.SMSCodeUtils;
import net.tv90.volunteerserveradmin.utils.entity.Assert;
import net.tv90.volunteerserveradmin.utils.entity.SMSCode;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class SMSCodeManager {

    @Resource
    private SMSCodeUtils smsCodeUtils;

    @Resource
    private MailManager mailManager;

    /**
     * 向邮箱地址发送验证码
     * @param email 邮箱地址
     */
    public void sendCodeToEmail(String email) {
        mailManager.sendCodeMailTo(email, smsCodeUtils.generateCodeForEmail(email));
    }

    public boolean checkCodeForTel(SMSCode smsCode) {
        String tel = smsCode.getSms();
        String code = smsCode.getCode();
        if(tel == null || code == null) {
            return false;
        }
        String cacheCode = smsCodeUtils.getCodeFromCacheForTel(tel);
        return Objects.equals(code, cacheCode);
    }


    public boolean checkCodeForEmail(SMSCode smsCode) {
        String email = smsCode.getSms();
        String code = smsCode.getCode();
        if(email == null || code == null) {
            return false;
        }
        String cacheCode = smsCodeUtils.getCodeFromCacheForEmail(email);
        return Objects.equals(code, cacheCode);
    }

    public void sendResetPasswordCodeToEmail(String email) {
        String resetPasswordCode = smsCodeUtils.generateCodeForEmail(email);
        mailManager.sendResetPasswordCodeMailTo(email, resetPasswordCode);
    }
}
